//
//  BSMenueViewController.m
//  HonlivApp
//
//  Created by admin on 2017/6/23.
//  Copyright © 2017年 honliv. All rights reserved.
//

#import "BSMenueViewController.h"

NSString *const reuseHeaderId = @"headerId";
NSString *const reuseFooterId = @"footerId";
NSString *const commonItemId = @"commonItemId";

@interface BSMenueViewController ()

@end

@implementation BSMenueViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getDatasArray];
}
-(void)getDatasArray{
    
}
-(void)getDataArray:(void(^)(GetDatas))doGet{
    GetDatas handler = ^(NSArray *array){
        _dataArray = [NSArray arrayWithArray:array];
        [self initUI];
    };
    doGet(handler);
}

- (void)initUI {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 1;
    CGFloat width = (kScreenW-10-15)/4;
    layout.itemSize = CGSizeMake(width, 85);
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH-64) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [self registerClass];
    [self.view addSubview:_collectionView];
}
//用类名注册
-(void)registerClass{
    
    [_collectionView registerClass:[HCommonItem class] forCellWithReuseIdentifier:commonItemId];
    [_collectionView registerClass:[SectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:reuseHeaderId];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:reuseFooterId];

}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return _dataArray.count;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSArray *arr = _dataArray[section][@"contents"];
    return arr.count;
}
//头尾视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if([kind isEqualToString:UICollectionElementKindSectionHeader]){
        SectionReusableView *cell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerId" forIndexPath:indexPath];
        
        cell.title = _dataArray[indexPath.section][@"title"];
        cell.getLine = NO;
        return cell;
    }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
        UICollectionReusableView *cell = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"footerId" forIndexPath:indexPath];
        cell.backgroundColor = Main_BGCOLOR;
        return cell;
    }
    return nil;
}
//设置headerView的size大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(kScreenW, 35);
}
//footerView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(kScreenW, 3);
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HCommonItem *cell = [HCommonItem cellWithCollectionView:collectionView indexPath:indexPath];
    NSArray *arr = _dataArray[indexPath.section][@"contents"];
    cell.dic = arr[indexPath.row];
    
    return cell;
}
-(void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
}


@end
